use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
use tauri::command;
use crate::types::ApiResponse;

#[command]
pub fn encode_base64(input: String) -> ApiResponse {
    ApiResponse::success(BASE64.encode(input.as_bytes()))
}

#[command]
pub fn decode_base64(input: String) -> ApiResponse {
    match BASE64.decode(input) {
        Ok(decoded) => {
            match String::from_utf8(decoded) {
                Ok(result) => ApiResponse::success(result),
                Err(e) => ApiResponse::error(format!("解码后的数据不是有效的 UTF-8 字符串: {}", e))
            }
        },
        Err(e) => ApiResponse::error(format!("Base64 解码失败: {}", e))
    }
} 